﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityModel;
using BusinessLayer;
using System.Collections;

namespace PL___Windows_Inteface
{
    public partial class ViewAllForm : Form
    {
        private string type;
        public ViewAllForm(String type)
        {
            InitializeComponent();
            this.type = type;
            if (type == "Private") {
                this.Text = "View of all private customers";
                type_list.Text = "List of Private Customers:";
                add_bt.Text = "Add New Private Customer";
                listView.Columns.Add("ImageColum", 0); //Not Show
                listView.Columns.Add("CPR", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Name", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Address", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Age", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Phone", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Sex", -2, HorizontalAlignment.Left);
                filllistViewPrivate();
            }
            else if (type == "Cars") {
                this.Text = "View of all cars in our database";
                type_list.Text = "List of Cars:";
                add_bt.Text = "Add New Car";
                listView.Columns.Add("ImageColum", 0); //Not Show
                listView.Columns.Add("Reg Number", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Fuel", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Price", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Color", -2, HorizontalAlignment.Left);
                listView.Columns.Add("State", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Type", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Num of Doors", -2, HorizontalAlignment.Left);
                filllistViewCars();
            }
            else if (type == "Trucks") {
                this.Text = "View of all Trucks in our database";
                type_list.Text = "List of Trucks:";
                add_bt.Text = "Add New Trucks";
                listView.Columns.Add("ImageColum", 0); //Not Show
                listView.Columns.Add("Reg Number", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Fuel", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Price", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Color", -2, HorizontalAlignment.Left);
                listView.Columns.Add("State", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Tonnage", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Num of Wheels", -2, HorizontalAlignment.Left);
                listView.Columns.Add("High", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Width", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Large", -2, HorizontalAlignment.Left);
                filllistViewTrucks();
            } if (type == "Business")
            {
                this.Text = "View of all private customers";
                type_list.Text = "List of Private Customers:";
                add_bt.Text = "Add New Private Customer";
                listView.Columns.Add("ImageColum", 0); //Not Show
                listView.Columns.Add("Company Name", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Company Id", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Address", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Phone", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Fax", -2, HorizontalAlignment.Left);
                listView.Columns.Add("Contact Person", -2, HorizontalAlignment.Left);
                filllistViewBusiness();
            }
        }

        public void filllistViewBusiness()
        {
            BL controller = new BL();
            foreach (Business b in controller.getBusinessCustomerList())
            {
                ListViewItem item = new ListViewItem(b.Customer.username);
                item.SubItems.Add(b.company_name);
                item.SubItems.Add(b.b_identifier);
                item.SubItems.Add(b.Customer.address_c);
                item.SubItems.Add(b.Customer.phone.ToString());
                item.SubItems.Add(b.fax.ToString());
                item.SubItems.Add(b.contact_person.ToString());
                listView.Items.Add(item);
            }
        }


        public void filllistViewTrucks()
        {
            BL controller = new BL();
            foreach (Truck t in controller.getTruckList())
            {
                ListViewItem item = new ListViewItem(t.Vehicle.reg_number);
                item.SubItems.Add(t.Vehicle.reg_number);
                item.SubItems.Add(controller.getFuelName(Convert.ToInt32(t.Vehicle.fuel)));
                item.SubItems.Add(t.Vehicle.price.ToString());
                item.SubItems.Add(t.Vehicle.colour);
                item.SubItems.Add(controller.getStateName(Convert.ToInt32(t.Vehicle.state)));
                item.SubItems.Add(t.tonnage.ToString());
                item.SubItems.Add(t.num_wheels.ToString());
                item.SubItems.Add(t.high.ToString());
                item.SubItems.Add(t.width.ToString());
                item.SubItems.Add(t.large.ToString());
                listView.Items.Add(item);
            }

        }

       
        public void filllistViewCars() { 
        BL controller = new BL();
            foreach (Car c in controller.getCarList())
            {
                ListViewItem item = new ListViewItem(c.Vehicle.reg_number);
                item.SubItems.Add(c.Vehicle.reg_number);
                item.SubItems.Add(controller.getFuelName(Convert.ToInt32(c.Vehicle.fuel)));
                item.SubItems.Add(c.Vehicle.price.ToString());
                item.SubItems.Add(c.Vehicle.colour);
                item.SubItems.Add(controller.getStateName(Convert.ToInt32(c.Vehicle.state)));
                item.SubItems.Add(controller.getCarTypeName(Convert.ToInt32(c.type_car)));
                item.SubItems.Add(c.doors_number.ToString());
                listView.Items.Add(item);            
            }
        
        }
        public void filllistViewPrivate(){
            BL controller = new BL();
            foreach (Private p in controller.getPrivateCustomerList()) {
                ListViewItem item = new ListViewItem(p.Customer.username);
                item.SubItems.Add(p.cpr.ToString());
                item.SubItems.Add(p.name);
                item.SubItems.Add(p.Customer.address_c);
                item.SubItems.Add(p.age.ToString());
                item.SubItems.Add(p.Customer.phone.ToString());
                item.SubItems.Add(p.sex);
                listView.Items.Add(item);            
            }  
        }

        private void ColumnClick(object o, ColumnClickEventArgs e)
        {
            // Set the ListViewItemSorter property to a new ListViewItemComparer 
            // object. Setting this property immediately sorts the 
            // ListView using the ListViewItemComparer object.
            listView.ListViewItemSorter = new ListViewItemComparer(e.Column);
        }
        class ListViewItemComparer : IComparer
        {
            private int col;
            public ListViewItemComparer()
            {
                col = 0;
            }
            public ListViewItemComparer(int column)
            {

                col = column;
            }
            public int Compare(object x, object y)
            {
                return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
            }
        }

        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListView.SelectedListViewItemCollection aux = listView.SelectedItems;
            if (aux.Count > 0)
            {
                if (type == "Private")
                {
                    (new ManagePrivate("VIEW",aux[0].Text)).ShowDialog();
                    listView.Items.Clear();
                    filllistViewPrivate();
                }
                else if (type == "Cars") {
                    (new ManageCar("VIEW", aux[0].Text)).ShowDialog();
                    listView.Items.Clear();
                    filllistViewCars();

                }
                else if (type == "Trucks")
                {
                    (new ManageTrucks("VIEW", aux[0].Text)).ShowDialog();
                    listView.Items.Clear();
                    filllistViewTrucks();
                }
                else if (type == "Business")
                {
                    (new ManageBusiness("VIEW", aux[0].Text)).ShowDialog();
                    listView.Items.Clear();
                    filllistViewBusiness();
                }
            }
        }

        private void add_item_bt_Click(object sender, EventArgs e)
        {
            if (type == "Private")
            {
                (new ManagePrivate("ADD", "")).ShowDialog();
                listView.Items.Clear();
                filllistViewPrivate();
                    
            }
            else if (type == "Cars")
            {
                (new ManageCar("ADD", "")).ShowDialog();
                listView.Items.Clear();
                filllistViewCars();

            }
            else if (type == "Trucks")
            {
                (new ManageTrucks("ADD","")).ShowDialog();
                listView.Items.Clear();
                filllistViewTrucks();
            }
            else if (type == "Business")
            {
                (new ManageBusiness("ADD", "")).ShowDialog();
                listView.Items.Clear();
                filllistViewBusiness();
            }
        }

    }
}
